﻿'         This file is Copyright © 2008-2012 Billy O'Neal and Aommaster
'                            All rights reserved.
'
' This file is released under the 2-Clause BSD license, included in LICENSE.TXT
'                           and on the MHWT website
Friend Class GlobalSettings
    Dim opts As GlobalOptions = GlobalData.options
    Private Sub populate()
        HandleBox.Text = opts.helperHandle
        greetingBox.Text = opts.greeting
        safeBox.Text = opts.safeheader
        SiggyBox.Text = opts.signature
        WaitBox.Text = opts.logwait
        StaleBox.Text = opts.logStale
        ResolvedBox.Text = opts.logResolved
        LogFormBox.Text = opts.logform
        backupdays.Text = opts.backupdays.ToString

        If opts.makebackups = True Then
            backupdays.Text = CStr(opts.backupdays)
            backup.Checked = True
        End If
    End Sub
    Private Sub HandleBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HandleBox.TextChanged
        opts.helperHandle = HandleBox.Text
    End Sub

    Private Sub greetingBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles greetingBox.TextChanged
        opts.greeting = greetingBox.Text
    End Sub

    Private Sub safeBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles safeBox.TextChanged
        opts.safeheader = safeBox.Text
    End Sub

    Private Sub SiggyBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SiggyBox.TextChanged
        opts.signature = SiggyBox.Text
    End Sub

    Private Sub WaitBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WaitBox.TextChanged
        opts.logwait = WaitBox.Text
    End Sub

    Private Sub StaleBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StaleBox.TextChanged
        opts.logStale = StaleBox.Text
    End Sub

    Private Sub ResolvedBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResolvedBox.TextChanged
        opts.logResolved = ResolvedBox.Text
    End Sub
    Private Sub GlobalSettings_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        RemoveHandler opts.Modified, AddressOf populate
    End Sub

    Private Sub GlobalSettings_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        populate()
        AddHandler opts.Modified, AddressOf populate
    End Sub

    Private Sub defaultBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles defaultBtn.Click
        opts.reset()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogFormBox.TextChanged
        opts.logform = LogFormBox.Text
    End Sub

    Private Sub backup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backup.CheckedChanged
        If backup.Checked = True Then
            backupdays.Enabled = True
            opts.makebackups = True

            Try
                Dim regKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("SOFTWARE\BillyOneal\MHWT\", True)
                Dim backupdate As Date = CDate(regKey.GetValue("Date"))
                If backupdate.AddDays(CDbl(backupdays.Text)) < Date.Now Then

                    regKey.SetValue("Date", CStr(Date.Now))
                    PrimaryUI.BackupToolStripMenuItem_Click(sender, e)
                End If
            Catch ex As Exception
                MsgBox("Error making backup")
            End Try
        Else
            backupdays.Enabled = False
            opts.makebackups = False
        End If
    End Sub

    Private Sub backupdays_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backupdays.TextChanged
        opts.backupdays = CInt(backupdays.Text)
    End Sub
End Class
